package edu.sjb.map;

import java.util.ArrayList;

import edu.sjb.Drawable;
import edu.sjb.characters.GameCharacter;
import edu.sjb.characters.Mario;
import edu.sjb.constants.ImageConstants;
import edu.sjb.constants.ScoreConstants;

public class BottomlessPit extends MapObject {
	public BottomlessPit() {
		super();
	}

	@Override
	public void hitAgainstMario(Mario m) {
		boolean hasToDie = false;
		if (!game().isMarioDead()) {
			if (m.posX() >= posX()) {
				if (m.posX() + 30 <= posX() + 32) {
					hasToDie = true;
				}
				else {
					if (isPit(false)) {
						hasToDie = true;
					}
				}
			}
			else {
				if (isPit(true)) {
					hasToDie = true;
				}
			}
		}
		if (hasToDie) {
			game().changeScore(ScoreConstants.BOTTOMLESS_PIT);
			m.die();
		}

	}

	@Override
	public void characterCollide(GameCharacter gc) {

		if (gc.posX() >= posX()) {
			if (gc.posX() + 30 <= posX() + 32) {
				gc.die();
			}
			else {
				if (isPit(false)) {
					gc.die();
				}
			}
		}
		else {
			if (isPit(true)) {
				gc.die();
			}
		}
	}

	@Override
	public void run() {
	}

	protected Object clone() {
		BottomlessPit newBP = new BottomlessPit();
		Drawable newImg = new Drawable(ImageConstants.BOTTOMLESS_PIT, false);
		newBP.setImage(newImg);
		return newBP;
	}

	private boolean isPit(boolean checkLeft) {
		boolean isPit = false;
		ArrayList<MapObject> otherObjects = game().getObjects();
		GameEntity adjacent = null;
		for (int k = 0; k < otherObjects.size(); k++) {
			adjacent = otherObjects.get(k);
			if (checkLeft) {
				if ((adjacent.posX() == posX() - 32) && (adjacent.posY() > 512)) {
					isPit = true;
				}
			}
			else {
				if ((adjacent.posX() == posX() + 32) && (adjacent.posY() > 512)) {
					isPit = true;
				}
			}
		}
		return isPit;
	}
}
